package Server.info;

import bean.novelBean;
import dao.Db;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

public class novelInfoServer {
    private HttpSession session;
    private HttpServletRequest request;
    private HttpServletResponse response;

    private novelBean novel;
    public novelInfoServer(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        this.request = request;
        this.response = response;
        session = request.getSession();

    }
    public void queryNovelBean() throws SQLException, IOException {
        this.novel = getNovelBean();
        session.setAttribute("novel",novel);
    }
    private novelBean getNovelBean() throws IOException, SQLException {
        Db db=new Db();
        String sql = "select * from tb_novel where novelNumber=?";
        String novelNumber = request.getParameter("novelNumber");//从地址栏中获取要看的小说编号
        ResultSet rs = db.select(sql,novelNumber);
        session.setAttribute("novelNumberCurrent",novelNumber);
        if(rs==null)
            throw new IOException("db error");
        if (rs.next() == false)	{						//尝试读取下一条记录，如果未读到记录
            db.close();
            throw new IOException("select is null");
        }
        novelBean novel =new novelBean();
        novel.setNovelName(rs.getString("novelName"));
        novel.setNovelNumber(novelNumber);
        novel.setNovelImage(rs.getString("novelImage"));
        novel.setNovelIntroduce(rs.getString("novelIntroduce"));
        return novel;
    }
    public novelBean getNovel() {
        return novel;
    }

    public void setNovel(novelBean novel) {
        this.novel = novel;
    }
}
